<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">
    <title>渠道推广数据</title>
    <link rel="stylesheet" href="../css/base.css">
    <script src="../js/jquery-1.11.3.min.js" ></script>
    <script src="../js/paramsTool.js" ></script>
    <script src="../js/layer.js" ></script>

</head>
<style type="text/css">
    .top{width:100%; height:60px; line-height:60px; text-align:center; background:#CB3536; color:#fff; font-size:18px;}
    .top #extract{position: absolute; right: 5px;top: 0px;font-size:14px; height: 60px; line-height: 60px; width: 100px;}
    .top #cashList{position: absolute; left: 5px;top: 0px;font-size:14px; height: 60px; line-height: 60px; width: 100px;}
    .listnum1{background:#f7f7f7;width:100%;height:30px; line-height:30px;border-top:1px solid #ddd;border-bottom:1px solid #ddd;}
    .listnum1 .left{float:left;width:40%;color:#898989;margin-left:5%;font-size:14px;}
    .listnum1 .right{float:left;width:50%;color:#898989;font-size:14px;}
    /***战友团明细***/
    .infoline_bands{width:100%;height:58px;padding:1px 0;border-bottom: solid #f0efef 1px; display:block; overflow:hidden;background:#fff; font-weight:100;}
    .infoline_bands .infopic{width:80px; float:left;line-height:58px;  padding: 0 5px;}
    .infoline_bands .infopic img{width:40px; height:40px;border-radius:100%; border:1px solid #CCC;margin:3px 0 0 0px;}
    .infoline_bands .infotxt{float:left; text-align:left; margin-top: 7px; line-height: 1.5; padding: 0 5px; }
    .infoline_bands .infotxt .name{padding-top:5px;text-align:left; font-weight:bold; font-size:12px;float:left;overflow:hidden; color:#000;}

    .infoline_bands .infotxt .time{padding:0px 2px 0 0; clear:both; min-height:18px;text-align:left; font-size:10px; float:left;display:block; font-weight:bold; overflow:hidden;}
    .sort{width:100%; text-align:center; font-size:13px; color:#333; height:40px; line-height:40px;border-bottom:0px solid #ddd; background:#f7f7f7;}
    .sort li{width:33.3%;float:left;}
    .sort .sel{color:#cc3435; font-weight:bold; border-bottom:1px solid#cc3435;}
    .marLeft{margin:10px;}
    .marLeft label{color:#d43f3a}
    .btnClass{margin: 15px auto;width: 90%;height: 35px;line-height: 35px;text-align: center;background-color: #ea4821;font-size: 17px;color: #FFF;border-radius: 5px;}

</style>
<script>
    var host = window.location.origin;
    var params = queryString();
    var channel = params.channel;
    if(!channel){
        alert('暂无权限!');
    }
    var reqUrl = '/getChannelUser';
    //dataNum
    var coinTotal = 0;
    var todUser = 0;
    var payUser = 0;
    $(function () {
        getData();

        $(".sort li").click(function() {

            $(this).siblings('li').removeClass('sel');  // 删除其他兄弟元素的样式

            $(this).addClass('sel');                    // 添加当前元素的样式

            reqUrl= $(".sel").data('postid');
            if(reqUrl == '/getChannelUser'){
                $('#topName').html('渠道玩家总数:');
                $('#leftTxt').html('渠道付费玩家:');
                $('#rightTxt').html('今日新增:');
                $('#rightTxt2').html('付费:');
            }else if(reqUrl == '/getChannelOrders'){
                $('#topName').html('所有订单');
                $('#leftTxt').html('累计总数:');
                $('#rightTxt').html(null);
                $('#rightTxt2').html(null);
            }else {
                $('#topName').html('所有提现');
                $('#leftTxt').html('累计总数:');
                $('#rightTxt').html(null);
                $('#rightTxt2').html(null);
            }

            $("#band").empty();
             coinTotal = 0;
             todUser = 0;
             payUser = 0;
            getData();
        });

        $("#cashList").click(function () {
            location.href = host+'/view/channel_PostData.html?channel='+channel;
        });

        $("#extract").click(function () {
            balance();
        });

        function getData(){
            if(!channel || channel.indexOf('popularize') == -1){
                alert('暂无权限!!!')
            }else {
                var layInd = layer.open({
                    type: 2,
                    content:'加载中...',
                    success : function () {
                        $.ajax({
                            url: host+ reqUrl,
                            type: "get",
                            dataType:'json',
                            async:true,//请求是否异步，默认为异步，这也是ajax重要特性
                            cache:false,
                            headers: {"content-type":"application/json","channel":channel},
                            success:function(data){
                                if(data.code){
                                    layer.close(layInd);        //关闭遮罩
                                    alert('error:'+data.info);
                                }else {
                                    var lists = data.lists;
                                    $('#total').html(lists.length);
                                    if(reqUrl == '/getChannelUser'){
                                        for(var i = 0;i<lists.length;i++){
                                            if(new Date().toDateString() == new Date(lists[i].regDate).toDateString()){
                                                todUser++;
                                                if(lists[i].coin) payUser++;
                                            }
                                            var infoBan=$("<div></div>").addClass('infoline_bands');
                                            var infopic=$("<div></div>").addClass('infopic').appendTo(infoBan);
                                            var imgHead;
                                            if(lists[i].coin){
                                                coinTotal++;
                                                imgHead = $("<div style='color: red; font-size: 14px; font-weight: 600;'>金币: "+lists[i].coin+"</div>").appendTo(infopic);
                                            }
                                            imgHead = $("<div>金币: 0</div>").appendTo(infopic);
                                            var infotxt = $("<div></div>").addClass('infotxt').appendTo(infoBan);
                                            var name = $("<div>"+lists[i].sid+"</div>").addClass('name').appendTo(infotxt);
                                            var time = $("<div>"+dateFormate(lists[i].regDate)+"</div>").addClass('time').appendTo(infotxt);
                                            infoBan.appendTo($('#band'));
                                        }
                                        $('#coinTotal').html(coinTotal);
                                        $('#todUser').html(todUser);
                                        $('#payUser').html(payUser);
                                    }else if(reqUrl == '/getChannelOrders'){
//                                        console.log('reqUrl data:',JSON.stringify(data));
                                        for(var i = 0;i<lists.length;i++){
                                            coinTotal+= lists[i].total_fee;
                                            var infoBan=$("<div></div>").addClass('infoline_bands');
                                            var infopic=$("<div></div>").addClass('infopic').appendTo(infoBan);


                                            var imgHead = $("<div style='color: red; font-size: 14px; font-weight: 600;'>金额: "+lists[i].total_fee/100+"元</div>").appendTo(infopic);

                                            var infotxt = $("<div></div>").addClass('infotxt').appendTo(infoBan);
                                            var name = $("<div>"+lists[i].sid+"</div>").addClass('name').appendTo(infotxt);
                                            var time = $("<div>"+lists[i].regChannel+"</div>").addClass('time').appendTo(infotxt);
                                            infoBan.appendTo($('#band'));
                                        }
                                        $('#coinTotal').html(coinTotal/100);
                                        $('#todUser').html(null);
                                        $('#payUser').html(null);
                                    }else {
//                                        console.log('reqUrl data:',JSON.stringify(data));
                                        for(var i = 0;i<lists.length;i++){
                                            coinTotal+= lists[i].rmb;
                                            var infoBan=$("<div></div>").addClass('infoline_bands');
                                            var infopic=$("<div></div>").addClass('infopic').appendTo(infoBan);
                                            var imgHead = $("<div style='color: red; font-size: 14px; font-weight: 600;'> 金额:"+lists[i].rmb/100+"元</div>").appendTo(infopic);
                                            var infotxt = $("<div></div>").addClass('infotxt').appendTo(infoBan);
                                            var name = $("<div>"+lists[i].sid+"</div>").addClass('name').appendTo(infotxt);
                                            var time = $("<div>"+lists[i].regChannel+"</div>").addClass('time').appendTo(infotxt);
                                            infoBan.appendTo($('#band'));
                                        }
                                        $('#coinTotal').html(coinTotal/100);
                                        $('#todUser').html(null);
                                        $('#payUser').html(null);
                                    }


                                    layer.close(layInd);        //关闭遮罩
                                }
                            },
                            error:function() {
                                layer.close(layInd);        //关闭遮罩
                                alert('获取数据失败,请重试!');
                            }

                        });

                    }
                });
            }
        }

        function balance(){

            $.ajax({
                url: host+ "/getChannelWorth",
                type: "get",
                dataType:'json',
                async:true,//请求是否异步，默认为异步，这也是ajax重要特性
                cache:false,
                headers: {"content-type":"application/json","channel":channel},
                success:function(data){
                    console.log('data:',JSON.stringify(data));
                    if(data.code){
                        alert('error:'+data.info);
                    }else {
                        data = data.data;
                        var rate = 0.75;
                        if(data.totalCoin > 55) rate = 0.88;
                        var coinCash =  parseInt(data.totalCoin * rate);
                        var rmbNum =(data.totalFee/100 - data.totalRmb/100 - coinCash);
                        if(rmbNum < 100) {
                            rmbNum = (rmbNum*0.5).toFixed(2);
                        }else {
                            rmbNum = (rmbNum*0.6).toFixed(2);
                        }
//                        console.log('coinCash:',data.totalCoin);
                        layer.open({
                            type: 1
                            ,content:'<div class="marLeft">' +
                            '<div style="text-align: center">规则: 金额 = (订单总量-渠道金币总量价值-提现总量) / 2</div><br>'+
                            '<div>当前金币总量:<label> '+data.totalCoin+ '</label>个 合计值:<label id="coinCash"> '+coinCash+'</label>元</div>'+
                            '<div>订单总量:<label>'+data.totalFee/100+'</label>元</div>'+
                            '<div>提现总量: <label>'+data.totalRmb/100+'</label>元</div>'+
                            '<div>可提现金额:<label>'+rmbNum+'</label>元</div>'+
                            '<div class="btnClass">管理员结算</div>'+
                            '</div>'
                            ,anim: 'up'
                            ,style: 'position:fixed; bottom:0; left:0; width: 100%; height: 200px; padding:10px 0; border:none;'
                        });
                        var openid = getCookie('wxopenid');// || 'o4AyEt2gypi_z981XBQzM5in0q_U';
                        if(openid != 'o4AyEt_EpmY83VE17bCOJ-t8MIEg' && openid != 'o4AyEt2gypi_z981XBQzM5in0q_U'){
                            $(".btnClass").hide();
                        }else {
                            $(".btnClass").show();
                        }
                        $(".btnClass").click(function() {
                            if(!openid){
//                                getWxCode();
                                openid = getCookie('wxopenid');
                            }
                            $.ajax({
                                url: host + "/listChannel",
                                type: "POST",
                                dataType: 'json',
                                async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                                cache: false,
                                headers: {"content-type": "application/json", "channel": channel,"openid":openid},
                                data:JSON.stringify({"coinNum":data.totalCoin,"coinValue":coinCash,"orderNum":data.totalFee/100,"cashNum":data.totalRmb/100,"dividedRmb":rmbNum}),
                                success: function (data) {
                                    if(data.code){
                                        alert('error:'+data.info);
                                    }else {
                                        layer.open({
                                            content: '结算记录成功!'
                                            ,skin: 'msg'
                                            ,time: 1.5 //2秒后自动关闭
                                        });
                                    }
                                },
                                error: function () {
                                    alert('记录结算失败,请重试!');
                                }
                            });

                        });

                    }
                },
                error:function() {
                    alert('获取数据失败,请重试!');
                }
            });
        }
    });

</script>
<body>
<!--顶部bar-->
<div class="top">
   <label id="topName"> 渠道玩家总数</label><label id="total" style="font-weight: 600">0</label>
    <div id="cashList">结算历史</div>
    <div id="extract">分成金额</div>
</div>
<!--顶部bar结束-->
<div class="listnum1">
    <div class="left">
        <label id="leftTxt"> 渠道付费玩家:</label> <label id="coinTotal" style="color:red;font-weight: 600">0</label>
    </div>
    <div class="right">
        <label id="rightTxt"> 今日新增:</label> <label id="todUser" style="color:red;font-weight: 600">0</label>  <label id="rightTxt2">付费:</label> <label id="payUser" style="color: red;font-weight: 600"></label>
    </div>
    <div class="clear"></div>
    <ul class="sort">
        <li class=" sel" data-postid="/getChannelUser">渠道用户</li>
        <li data-postid="/getChannelOrders">渠道订单</li>
        <li data-postid="/getChannelCash">渠道提现</li>
    </ul>

</div>
<div id="band">

</div>
</body>
</html>
